//
//  RODatePickerView.m
//  Runone
//
//  Created by runone02 on 16/12/14.
//  Copyright © 2016年 runone. All rights reserved.
//

#import "RODatePickerView.h"
#import "RODateHelper.h"
#import "NSDate+ROCaculateTime.h"
#import "Masonry.h"

#define kDefaultMinLimitedDate      @"1990-01-01 00:00:00"
#define kDefaultMaxLimitedDate      @"2040-12-31 23:59:59"
#define kMonthCountOfEveryYear      12
#define kHourCountOfEveryDay        24
#define kMinuteCountOfEveryHour     60
#define ksecondCountOfEveryMin      60
#define kRowDisabledStatusColor     [UIColor redColor]
#define kRowNormalStatusColor       [UIColor blackColor]
#define kWidthOfTotal               self.frame.size.width - 10
#define kHeightOfButtonContentView  35.0
#define kButtonNormalStatusColor    [UIColor colorWithRed:0.0 green:0.5 blue:1.0 alpha:1.0]
#define kTitleFont                  15

@interface RODatePickerView (){
    
    
    RODatePickerDataModel *_datePickerDateMinLimited     ;//最小时间
    RODatePickerDataModel *_datePickerDateMaxLimited     ;//最大时间
    RODatePickerDataModel *_datePickerDateDefaultScrollTo;//默认滚动到时间的模型
    
    //储存时间日期的数据源数组
    NSMutableArray *_arrYear  ;
    NSMutableArray *_arrMonth ;
    NSMutableArray *_arrDay   ;
    NSMutableArray *_arrHour  ;
    NSMutableArray *_arrMinute;
    NSMutableArray *_arrSecond;
    
    //数据源数组中选中的元素下标
    NSInteger _yearIndex  ;
    NSInteger _monthIndex ;
    NSInteger _dayIndex   ;
    NSInteger _hourIndex  ;
    NSInteger _minuteIndex;
    NSInteger _secondIndex;
}

@property (nonatomic, strong)  UIPickerView *pickV;    // 时间选择视图
@property (nonatomic, strong)  UIView  *toolBar;       // 工具条
@property (nonatomic, strong)  UIButton *cancleBtn;    // 取消
@property (nonatomic, strong)  UIButton *confirmBtn;   // 确认
@property (nonatomic, strong)  UILabel *daylab ;
@property (nonatomic, strong)  UILabel *monLab ;
@property (nonatomic, strong)  UILabel *yearLab;
@property (nonatomic, strong)  UILabel *hourLab;
@property (nonatomic, strong)  UILabel *minLab ;
@property (nonatomic, strong)  UILabel *secLab ;


@end


@implementation RODatePickerView

- (instancetype)init {
    if (self = [super init]) {
        self.backgroundColor = [UIColor yellowColor];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        [self configUI];
        [self loadData];
        [self layoutUI];
        [self scrollToDateIndexPosition];
    }
    return self;
}

#pragma mark - 初始化
- (instancetype)initWithFrame:(CGRect)frame delegate:(id<RODatePickViewDelegate>)delegate datePickerStyle:(RODatePickerStyle)datePickerStyle datePickViewPushModel:(RODatePickerViewPushModel)viewPushModel {
    _delegate        = delegate;
    _pickerStyle     = datePickerStyle;
    _viewPushModel   = viewPushModel;
    return [self initWithFrame:frame];
}

#pragma mark - 重写
- (void)setMinLimitedDate:(NSDate *)minLimitedDate {
    _minLimitedDate = minLimitedDate;
    if (_minLimitedDate && !_defaultLimitedDate) {
        _defaultLimitedDate = _minLimitedDate;
    }
}

- (void) configUI {
    _pickV = [[UIPickerView alloc] init];
    _pickV.backgroundColor = [UIColor colorWithRed:235.0 / 255 green:235.0 / 255 blue:241.0 / 255 alpha:1];
    _pickV.dataSource = self;
    _pickV.delegate   = self;
    [self addSubview:_pickV];
    
    _toolBar  = [[UIView alloc] init];
//    _toolBar.backgroundColor = [UIColor yellowColor];
    [self addSubview:_toolBar];
    
    _cancleBtn = [[UIButton alloc] init];
    [_cancleBtn setTitle:@"取消" forState:UIControlStateNormal];
    [_cancleBtn setTitleColor:[UIColor colorWithRed:82.0 / 255 green:155.0 / 255 blue:255.0 / 255 alpha:1] forState:UIControlStateNormal];
    [_cancleBtn addTarget:self action:@selector(cancel:) forControlEvents:UIControlEventTouchUpInside];
    [_toolBar addSubview:_cancleBtn];
    
    _confirmBtn = [[UIButton alloc] init];
    [_confirmBtn setTitle:@"确定" forState:UIControlStateNormal];
    [_confirmBtn setTitleColor:[UIColor colorWithRed:82.0 / 255 green:155.0 / 255 blue:255.0 / 255 alpha:1] forState:UIControlStateNormal];
    [_confirmBtn addTarget:self  action:@selector(confirm:) forControlEvents:UIControlEventTouchUpInside];
    [_toolBar addSubview: _confirmBtn];
    
    _daylab = [[UILabel alloc] init];
    _daylab.text     = @"日";
    _daylab.font     = [UIFont systemFontOfSize:kTitleFont];
    [_pickV addSubview: _daylab];
    
    
    _monLab = [[UILabel alloc] init];
    _monLab.text     = @"月";
     _monLab.font     = [UIFont systemFontOfSize:kTitleFont];
    
    [_pickV addSubview:_monLab];
    
    _yearLab = [[UILabel alloc] init];
    _yearLab.text     = @"年";
     _yearLab.font     = [UIFont systemFontOfSize:kTitleFont];
    [_pickV addSubview:_yearLab];

    _hourLab = [[UILabel alloc] init];
    _hourLab.text     = @"时";
     _hourLab.font     = [UIFont systemFontOfSize:kTitleFont];
    [_pickV addSubview:_hourLab];
    
    _minLab = [[UILabel alloc] init];
    _minLab.text     = @"分";
     _minLab.font     = [UIFont systemFontOfSize:kTitleFont];
    [_pickV addSubview:_minLab];
    
    _secLab = [[UILabel alloc] init];
    _secLab.text     = @"秒";
     _secLab.font     = [UIFont systemFontOfSize:kTitleFont];
    [_pickV addSubview:_secLab];
}

- (void) layoutUI {
    __weak __typeof(self)weakSelf = self;
    [_toolBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.mas_top);
        make.left.mas_equalTo(weakSelf.mas_left);
        make.right.mas_equalTo(weakSelf.mas_right);
        make.height.mas_equalTo(kHeightOfButtonContentView);
    }];
    
    [_cancleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf.toolBar.mas_left).with.offset(8);
        make.top.mas_equalTo(weakSelf.toolBar.mas_top);
        make.bottom.mas_equalTo(weakSelf.toolBar.mas_bottom);
        make.width.mas_greaterThanOrEqualTo(30);
    }];
    
    [_confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(weakSelf.toolBar.mas_right).with.offset(-8);
        make.top.mas_equalTo(weakSelf.toolBar.mas_top);
        make.bottom.mas_equalTo(weakSelf.toolBar.mas_bottom);
        make.width.mas_greaterThanOrEqualTo(30);
    }];
    
    [_pickV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.toolBar.mas_bottom);
        make.left.mas_equalTo(weakSelf.mas_left);
        make.right.mas_equalTo(weakSelf.mas_right);
        make.height.mas_equalTo(216);
//        make.bottom.mas_equalTo(weakSelf.mas_bottom);
    }];
    
    [_daylab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakSelf.pickV.mas_centerY);
        make.centerX.mas_equalTo(weakSelf.pickV.mas_centerX).with.offset(-2);
        make.width.mas_equalTo(15);
    }];
    
    [_monLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakSelf.pickV.mas_centerY);
        make.right.mas_equalTo(weakSelf.daylab.mas_left).with.offset(-32);
        make.width.mas_equalTo(15);
    }];
    
    [_yearLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakSelf.pickV.mas_centerY);
        make.right.mas_equalTo(weakSelf.monLab.mas_left).with.offset(-30);
        make.width.mas_equalTo(15);
    }];

    [_hourLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakSelf.pickV.mas_centerY);
        make.left.mas_equalTo(weakSelf.daylab.mas_right).with.offset(28);
        make.width.mas_equalTo(15);
    }];
    
    [_minLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakSelf.pickV.mas_centerY);
        make.left.mas_equalTo(weakSelf.hourLab.mas_right).with.offset(28);
        make.width.mas_equalTo(15);
    }];
    
    [_secLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakSelf.pickV.mas_centerY);
        make.left.mas_equalTo(weakSelf.minLab.mas_right).with.offset(28);
        make.width.mas_equalTo(15);
    }];
}

- (void)bringLabelToFront {
    [_yearLab bringSubviewToFront:_pickV];
    [_monLab  bringSubviewToFront:_pickV];
    [_daylab  bringSubviewToFront:_pickV];
    [_hourLab bringSubviewToFront:_pickV];
    [_minLab  bringSubviewToFront:_pickV];
    [_secLab  bringSubviewToFront:_pickV];
}

#pragma mark -获取对应的月份的天数
- (NSUInteger)daysOfMonth {
    NSString *dateStr = [NSString stringWithFormat:@"%@-%@-01 00:00", _arrYear[_yearIndex], _arrMonth[_monthIndex]];
    return [[RODateHelper dateFromString:dateStr withFormat:@"yyyy-MM-dd HH:mm"] daysOfMonth];
}
#pragma mark -刷新天数数组
- (void)reloadDayArray {
    _arrDay = [[NSMutableArray alloc] init];
    for (NSUInteger i = 1, len = [self daysOfMonth]; i <= len; i++) {
        [_arrDay addObject:[NSString stringWithFormat:@"%02ld", (long)i]];
    }
}
#pragma mark -
- (void)loadData {
    // 初始化最小和最大限制时间、滚动到指定时间实体对象实例
    if (!_minLimitedDate) {
        _minLimitedDate = [RODateHelper dateFromString:kDefaultMinLimitedDate withFormat:nil];
    }
    _datePickerDateMinLimited = [[RODatePickerDataModel alloc] initWithDate:_minLimitedDate];
    
    if (!_maxLimitedDate) {
        _maxLimitedDate = [RODateHelper dateFromString:kDefaultMaxLimitedDate withFormat:nil];
    }
    _datePickerDateMaxLimited = [[RODatePickerDataModel alloc] initWithDate:_maxLimitedDate];
    
    // 滚动到指定时间；默认值为当前时间。如果是使用自定义时间小于最小限制时间，这时就以最小限制时间为准；如果是使用自定义时间大于最大限制时间，这时就以最大限制时间为准
    if (!_scrollToDate) {
        _scrollToDate = [RODateHelper localeDate];
    }
    if ([_scrollToDate compare:_minLimitedDate] == NSOrderedAscending) {
        _scrollToDate = _minLimitedDate;
    } else if ([_scrollToDate compare:_maxLimitedDate] == NSOrderedDescending) {
        _scrollToDate = _maxLimitedDate;
    }
    _datePickerDateDefaultScrollTo = [[RODatePickerDataModel alloc] initWithDate:_scrollToDate];
    // 年
    _arrYear = [NSMutableArray new];
    for (NSInteger beginVal = [_datePickerDateMinLimited.yearStr integerValue], endVal = [_datePickerDateMaxLimited.yearStr integerValue]; beginVal <= endVal; beginVal++) {
        [_arrYear addObject:[NSString stringWithFormat:@"%ld", (long)beginVal]];
    }
    _yearIndex = [_datePickerDateDefaultScrollTo.yearStr integerValue] - [_datePickerDateMinLimited.yearStr integerValue];
    // 月
    _arrMonth = [[NSMutableArray alloc] initWithCapacity:kMonthCountOfEveryYear];
    for (NSInteger i = 1; i <= kMonthCountOfEveryYear; i++) {
        [_arrMonth addObject:[NSString stringWithFormat:@"%02ld", (long)i]];
    }
    _monthIndex = [_datePickerDateDefaultScrollTo.monthStr integerValue] - 1;
    // 日
    [self reloadDayArray];
    _dayIndex = [_datePickerDateDefaultScrollTo.dayStr integerValue] - 1;
    // 时
    _arrHour = [[NSMutableArray alloc] initWithCapacity:kHourCountOfEveryDay];
    for (NSInteger i = 0; i < kHourCountOfEveryDay; i++) {
        [_arrHour addObject:[NSString stringWithFormat:@"%02ld", (long)i]];
    }
    _hourIndex = [_datePickerDateDefaultScrollTo.hourStr integerValue];
    // 分
    _arrMinute = [[NSMutableArray alloc] initWithCapacity:kMinuteCountOfEveryHour];
    for (NSInteger i = 0; i < kMinuteCountOfEveryHour; i++) {
        [_arrMinute addObject:[NSString stringWithFormat:@"%02ld", (long)i]];
    }
    _minuteIndex = [_datePickerDateDefaultScrollTo.minStr integerValue];
    //秒
    _arrSecond = [[NSMutableArray alloc] initWithCapacity:ksecondCountOfEveryMin];
    for (NSInteger i = 0; i < ksecondCountOfEveryMin; i++) {
        [_arrSecond addObject:[NSString stringWithFormat:@"%02ld",(long)i]];
    }
}
#pragma mark - 滚动
- (void)scrollToDateIndexPosition {
    NSArray *arrIndex;
    switch (_pickerStyle) {
        case RODatePickerStyleYearMonthDayHourMinuteSecond: { //年月日时分秒
            arrIndex = @[
                         [NSNumber numberWithInteger:_yearIndex],
                         [NSNumber numberWithInteger:_monthIndex],
                         [NSNumber numberWithInteger:_dayIndex],
                         [NSNumber numberWithInteger:_hourIndex],
                         [NSNumber numberWithInteger:_minuteIndex],
                         [NSNumber numberWithInteger:_secondIndex]
                         ];
            break;
        }
        case RODatePickerStyleYearMonthDay: { //年月日
            arrIndex = @[
                         [NSNumber numberWithInteger:_yearIndex],
                         [NSNumber numberWithInteger:_monthIndex],
                         [NSNumber numberWithInteger:_dayIndex]
                         ];
            break;
        }
        case RODatePickerStyleMonthDayHourMinuteSecond: {//月日时分秒
            arrIndex = @[
                         [NSNumber numberWithInteger:_monthIndex],
                         [NSNumber numberWithInteger:_dayIndex],
                         [NSNumber numberWithInteger:_hourIndex],
                         [NSNumber numberWithInteger:_minuteIndex],
                         [NSNumber numberWithInteger:_secondIndex]
                         ];
            break;
        }
        case RODatePickerStyleHourMinuteSecond: {//时分秒
            arrIndex = @[
                         [NSNumber numberWithInteger:_hourIndex],
                         [NSNumber numberWithInteger:_minuteIndex],
                         [NSNumber numberWithInteger:_secondIndex]
                         ];
            break;
        }
    }
    for (NSUInteger i = 0, len = arrIndex.count; i<len; i++) {
        [_pickV selectRow:[arrIndex[i] integerValue] inComponent:i animated:YES];
    }
}
#pragma mark- 判断设置的最小时间是否在有效范围内
- (BOOL)validatedDate:(NSDate *)date {
    NSString *minDateStr = [NSString stringWithFormat:@"%@-%@-%@ %@:%@:%@",
                            _datePickerDateMinLimited.yearStr ,
                            _datePickerDateMinLimited.monthStr,
                            _datePickerDateMinLimited.dayStr  ,
                            _datePickerDateMinLimited.hourStr ,
                            _datePickerDateMinLimited.minStr  ,
                            _datePickerDateMinLimited.secondStr
                            ];
    
    return !([date compare:[RODateHelper dateFromString:minDateStr withFormat:nil]] == NSOrderedAscending ||
             [date compare:_maxLimitedDate] == NSOrderedDescending);
}
- (BOOL)canShowScrollToNowButton {
    return [self validatedDate:[RODateHelper localeDate]];
}
- (void)cancel:(UIButton *)sender {
    UIViewController *delegateVC = (UIViewController *)self.delegate;
    [delegateVC.view endEditing:YES];
    self.cancleBlock(sender);
}
#pragma mark - 滚动到当前时间
- (void)scrollToNowDateIndexPosition:(UIButton *)sender {
    [self scrollToDateIndexPositionWithDate:[RODateHelper localeDate]];
}
- (void)scrollToDateIndexPositionWithDate:(NSDate *)date {
    // 为了区别最大最小限制范围外行的标签颜色，这里需要重新加载所有组件列
    [_pickV reloadAllComponents];
    
    _scrollToDate = date;
    _datePickerDateDefaultScrollTo = [[RODatePickerDataModel alloc] initWithDate:_scrollToDate];
    _yearIndex        = [_datePickerDateDefaultScrollTo.yearStr integerValue] - [_datePickerDateMinLimited.yearStr integerValue];
    _monthIndex       = [_datePickerDateDefaultScrollTo.monthStr integerValue] - 1;
    _dayIndex         = [_datePickerDateDefaultScrollTo.dayStr integerValue] - 1  ;
    _hourIndex        = [_datePickerDateDefaultScrollTo.hourStr integerValue]     ;
    _minuteIndex      = [_datePickerDateDefaultScrollTo.minStr integerValue]      ;
    _secondIndex      = [_datePickerDateDefaultScrollTo.secondStr integerValue]   ;
    [self scrollToDateIndexPosition];
}
- (void)confirm:(UIButton *)sender {
    [self playDelegateAfterSelectedRow];
    [self cancel:sender];
}
#pragma mark -月份组件颜色
- (UIColor *)monthRowTextColor:(NSInteger)row {
    UIColor *color = kRowNormalStatusColor;
    NSString *dateStr = [NSString stringWithFormat:@"%@-%@-01 00:00",
                         _arrYear[_yearIndex],
                         _arrMonth[row]
                         ];
    NSDate *date = [RODateHelper dateFromString:dateStr withFormat:nil];
    
    NSString *minDateStr = [NSString stringWithFormat:@"%@-%@-01 00:00",
                            _datePickerDateMinLimited.yearStr,
                            _datePickerDateMinLimited.monthStr
                            ];
    
    if ([date compare:[RODateHelper dateFromString:minDateStr withFormat:nil]] == NSOrderedAscending ||
        [date compare:_maxLimitedDate] == NSOrderedDescending) {
        color = kRowDisabledStatusColor;
    }
    return color;
}
#pragma mark -天的组件颜色
- (UIColor *)dayRowTextColor:(NSInteger)row {
    UIColor *color = kRowNormalStatusColor;
    NSString *dateStr = [NSString stringWithFormat:@"%@-%@-%@ 00:00",
                         _arrYear[_yearIndex],
                         _arrMonth[_monthIndex],
                         _arrDay[row]
                         ];
    NSDate *date = [RODateHelper dateFromString:dateStr withFormat:nil];
    
    NSString *minDateStr = [NSString stringWithFormat:@"%@-%@-%@ 00:00",
                            _datePickerDateMinLimited.yearStr,
                            _datePickerDateMinLimited.monthStr,
                            _datePickerDateMinLimited.dayStr
                            ];
    
    if ([date compare:[RODateHelper dateFromString:minDateStr withFormat:nil]] == NSOrderedAscending ||
        [date compare:_maxLimitedDate] == NSOrderedDescending) {
        color = kRowDisabledStatusColor;
    }
    return color;
}
#pragma mark -小时组件的颜色
- (UIColor *)hourRowTextColor:(NSInteger)row {
    UIColor *color = kRowNormalStatusColor;
    NSString *dateStr = [NSString stringWithFormat:@"%@-%@-%@ %@:00",
                         _arrYear[_yearIndex],
                         _arrMonth[_monthIndex],
                         _arrDay[_dayIndex],
                         _arrHour[row]
                         ];
    NSDate *date = [RODateHelper dateFromString:dateStr withFormat:nil];
    
    NSString *minDateStr = [NSString stringWithFormat:@"%@-%@-%@ %@:00",
                            _datePickerDateMinLimited.yearStr,
                            _datePickerDateMinLimited.monthStr,
                            _datePickerDateMinLimited.dayStr,
                            _datePickerDateMinLimited.hourStr
                            ];
    
    if ([date compare:[RODateHelper dateFromString:minDateStr withFormat:nil]] == NSOrderedAscending ||
        [date compare:_maxLimitedDate] == NSOrderedDescending) {
        color = kRowDisabledStatusColor;
    }
    
    return color;
}
#pragma mark -分组件的颜色
- (UIColor *)minuteRowTextColor:(NSInteger)row {
    NSString *format = @"yyyy-MM-dd HH:mm:ss";
    UIColor *color = kRowNormalStatusColor;
    NSString *dateStr = [NSString stringWithFormat:@"%@-%@-%@ %@:%@:00",
                         _arrYear[_yearIndex],
                         _arrMonth[_monthIndex],
                         _arrDay[_dayIndex],
                         _arrHour[_hourIndex],
                         _arrMinute[row]
                         ];
    NSDate *date = [RODateHelper dateFromString:dateStr withFormat:format];
    
    NSString *minDateStr = [NSString stringWithFormat:@"%@-%@-%@ %@:%@:00",
                            _datePickerDateMinLimited.yearStr,
                            _datePickerDateMinLimited.monthStr,
                            _datePickerDateMinLimited.dayStr,
                            _datePickerDateMinLimited.hourStr,
                            _datePickerDateMinLimited.minStr
                            ];
    
    if ([date compare:[RODateHelper dateFromString:minDateStr withFormat:format]] == NSOrderedAscending ||
        [date compare:_maxLimitedDate] == NSOrderedDescending) {
        color = kRowDisabledStatusColor;
    }
    
    return color;
}

#pragma mark -秒组件的颜色
- (UIColor *)secondRowTextColor:(NSInteger)row {
    NSString *format = @"yyyy-MM-dd HH:mm:ss";
    UIColor *color = kRowNormalStatusColor;
    NSString *dateStr = [NSString stringWithFormat:@"%@-%@-%@ %@:%@:%@",
                         _arrYear[_yearIndex],
                         _arrMonth[_monthIndex],
                         _arrDay[_dayIndex],
                         _arrHour[_hourIndex],
                         _arrMinute[row],
                         _arrSecond[row]
                         ];
    NSDate *date = [RODateHelper dateFromString:dateStr withFormat:format];
    
    NSString *minDateStr = [NSString stringWithFormat:@"%@-%@-%@ %@:%@:%@",
                            _datePickerDateMinLimited.yearStr,
                            _datePickerDateMinLimited.monthStr,
                            _datePickerDateMinLimited.dayStr,
                            _datePickerDateMinLimited.hourStr,
                            _datePickerDateMinLimited.minStr,
                            _datePickerDateMinLimited.secondStr
                            ];
    
    if ([date compare:[RODateHelper dateFromString:minDateStr withFormat:format]] == NSOrderedAscending ||
        [date compare:_maxLimitedDate] == NSOrderedDescending) {
        color = kRowDisabledStatusColor;
    }
    
    return color;
}

#pragma mark - 执行 KMDatePickerDelegate 委托代理协议方法，用于回调传递参数
- (void)playDelegateAfterSelectedRow {
    if ([self.delegate respondsToSelector:@selector(datePicker:didSelectDate:)]) {
        [self.delegate datePicker:self
                    didSelectDate:_datePickerDateDefaultScrollTo];
    }
}
#pragma mark - UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    NSInteger numberOfComponents = 0;
    switch (_pickerStyle) {
        case RODatePickerStyleYearMonthDayHourMinuteSecond: { //年月日时分秒
            numberOfComponents = 6;
            break;
        }
        case RODatePickerStyleYearMonthDay: {   //年月日
            numberOfComponents = 3;
            break;
        }
        case RODatePickerStyleMonthDayHourMinuteSecond: { //月日时分秒
            numberOfComponents = 5;
            break;
        }
        case RODatePickerStyleHourMinuteSecond: {      //时分秒
            numberOfComponents = 3;
            break;
        }
    }
    return numberOfComponents;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    NSInteger numberOfRows = 0;
    switch (_pickerStyle) {
        case RODatePickerStyleYearMonthDayHourMinuteSecond: {
            switch (component) {  //年月日时分秒
                case 0:
                    numberOfRows = (_arrYear.count) ;
                    break;
                case 1:
                    numberOfRows = kMonthCountOfEveryYear ;
                    break;
                case 2:
                    numberOfRows = [self daysOfMonth] ;
                    break;
                case 3:
                    numberOfRows = kHourCountOfEveryDay ;
                    break;
                case 4:
                    numberOfRows = kMinuteCountOfEveryHour;
                    break;
                case 5:
                    numberOfRows = ksecondCountOfEveryMin ;
                    break;
            }
            [self bringLabelToFront];
            break;
        }
        case RODatePickerStyleYearMonthDay: {  //年月日
            switch (component) {
                case 0:
                    numberOfRows = _arrYear.count;
                    break;
                case 1:
                    numberOfRows = kMonthCountOfEveryYear;
                    break;
                case 2:
                    numberOfRows = [self daysOfMonth];
                    break;
            }
            break;
        }
        case RODatePickerStyleMonthDayHourMinuteSecond: { //月日时分秒
            switch (component) {
                case 0:
                    numberOfRows = kMonthCountOfEveryYear;
                    break;
                case 1:
                    numberOfRows = [self daysOfMonth];
                    break;
                case 2:
                    numberOfRows = kHourCountOfEveryDay;
                    break;
                case 3:
                    numberOfRows = kMinuteCountOfEveryHour;
                    break;
                case 4:
                    numberOfRows = ksecondCountOfEveryMin;
                    break;
            }
            break;
        }
        case RODatePickerStyleHourMinuteSecond: { //时分秒
            switch (component) {
                case 0:
                    numberOfRows = kHourCountOfEveryDay;
                    break;
                case 1:
                    numberOfRows = kMinuteCountOfEveryHour;
                    break;
                case 2:
                    numberOfRows = ksecondCountOfEveryMin;
                    break;
            }
            break;
        }
    }
    return numberOfRows;
}
#pragma mark - UIPickerViewDelegate
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    CGFloat width = 40.0;
    CGFloat widthOfAverage;
    
    
    switch (_pickerStyle) {
        case RODatePickerStyleYearMonthDayHourMinuteSecond: { //年月日时分秒
            
            widthOfAverage = kWidthOfTotal  / 6;
            switch (component) {
                case 0:
                    width = 58;
                    break;
                case 1:
                    width = 38;
                    break;
                case 2:
                    width = 38;
                    break;
                case 3:
                    width = 38 ;
                    break;
                case 4:
                    width = 38 ;
                    break;
             //   case 5:
             //       width = 38;
             //       break;
            }
            break;
        }
        case RODatePickerStyleYearMonthDay: {//年月日
            widthOfAverage = (kWidthOfTotal - 20.0 - 15.0) / 3;
            switch (component) {
                case 0:

                    break;
                case 1:
                    
                    break;
                case 2:

                    break;
            }
            break;
        }
        case RODatePickerStyleMonthDayHourMinuteSecond: { ////月日时分秒
            widthOfAverage = (kWidthOfTotal - 20.0) / 5;
            switch (component) {
                case 0:
                    
                    break;
                case 1:
                    
                    break;
                case 2:
                   
                    break;
                case 3:
                    
                    break;
                case 4:
                   
                    break;
            }
            break;
        }
        case RODatePickerStyleHourMinuteSecond: { //时分秒
            widthOfAverage = (kWidthOfTotal - 10.0) / 3;
            switch (component) {
                case 0:
                   
                    break;
                case 1:
                    
                    break;
                case 2:
                   
                    break;
            }
            break;
        }
    }
    return width;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return 30.0;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view {
    UILabel *lblCustom = (UILabel *)view;
    if (!lblCustom) {
        lblCustom = [UILabel new];
        lblCustom.textAlignment = NSTextAlignmentLeft;
        lblCustom.font = [UIFont systemFontOfSize:kTitleFont];
    }
    
    NSString *text;
    UIColor *textColor = kRowNormalStatusColor;
    switch (_pickerStyle) {
        case RODatePickerStyleYearMonthDayHourMinuteSecond: {
            switch (component) {
                case 0:
                    text = _arrYear[row];

                    break;
                case 1:
                    text = _arrMonth[row];
                    textColor = [self monthRowTextColor:row];
                 
                    break;
                case 2:
                    text = _arrDay[row];
                    textColor = [self dayRowTextColor:row];
              
                    break;
                case 3:
                    text = _arrHour[row];
                    textColor = [self hourRowTextColor:row];
          
                    break;
                case 4:
                    text = _arrMinute[row];
                    textColor = [self minuteRowTextColor:row];
                 
                    break;
                case 5:
                    text = _arrSecond[row];
                    textColor = [self secondRowTextColor:row];
                    break;
            }
            break;
        }
        case RODatePickerStyleYearMonthDay: {
            switch (component) {
                case 0:
                    text = _arrYear[row];
                    break;
                case 1:
                    text = _arrMonth[row];
                    textColor = [self monthRowTextColor:row];
                    break;
                case 2:
                    text = _arrDay[row];
                    textColor = [self dayRowTextColor:row];
                    break;
            }
            break;
        }
        case RODatePickerStyleMonthDayHourMinuteSecond: {
            switch (component) {
                case 0:
                    text = _arrMonth[row];
                    textColor = [self monthRowTextColor:row];
                    break;
                case 1:
                    text = _arrDay[row];
                    textColor = [self dayRowTextColor:row];
                    break;
                case 2:
                    text = _arrHour[row];
                    textColor = [self hourRowTextColor:row];
                    break;
                case 3:
                    text = _arrMinute[row];
                    textColor = [self minuteRowTextColor:row];
                    break;
                case 4:
                    text = _arrSecond[row];
                    textColor = [self secondRowTextColor:row];
                    break;
            }
            break;
        }
        case RODatePickerStyleHourMinuteSecond: {//时分秒
            switch (component) {
                case 0:
                    text = _arrHour[row];
                    textColor = [self hourRowTextColor:row];
                    break;
                case 1:
                    text = _arrMinute[row];
                    textColor = [self minuteRowTextColor:row];
                    break;
                case 2:
                    text = _arrSecond[row];
                    textColor = [self secondRowTextColor:row];
                    break;
            }
            break;
        }
    }
    lblCustom.text      = text;
    lblCustom.textColor = textColor;
    return lblCustom;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    switch (_pickerStyle) {
        case RODatePickerStyleYearMonthDayHourMinuteSecond: {//年月日时分秒
            switch (component) {
                case 0:
                    _yearIndex = row;
                    break;
                case 1:
                    _monthIndex = row;
                    break;
                case 2:
                    _dayIndex = row;
                    break;
                case 3:
                    _hourIndex = row;
                    break;
                case 4:
                    _minuteIndex = row;
                    break;
                case 5:
                    _secondIndex = row;
                    break;
            }
            if (component == 0 || component == 1) {
                [self reloadDayArray];
                if (_arrDay.count - 1 < _dayIndex) {
                    _dayIndex = _arrDay.count - 1;
                }
            }
            break;
        }
        case RODatePickerStyleYearMonthDay: {//年月日
            switch (component) {
                case 0:
                    _yearIndex = row;
                    break;
                case 1:
                    _monthIndex = row;
                    break;
                case 2:
                    _dayIndex = row;
                    break;
            }
            if (component == 0 || component == 1) {
                [self reloadDayArray];
                if (_arrDay.count-1 < _dayIndex) {
                    _dayIndex = _arrDay.count - 1;
                }
            }
            break;
        }
        case RODatePickerStyleMonthDayHourMinuteSecond: { //月日时分秒
            switch (component) {
                case 0:
                    _monthIndex = row;
                    break;
                case 1:
                    _dayIndex = row;
                    break;
                case 2:
                    _hourIndex = row;
                    break;
                case 3:
                    _minuteIndex = row;
                    break;
                case 4:
                    _secondIndex = row;
                    break;
            }
            if (component == 0) {
                [self reloadDayArray];
                if (_arrDay.count-1 < _dayIndex) {
                    _dayIndex = _arrDay.count - 1;
                }
            }
            break;
        }
        case RODatePickerStyleHourMinuteSecond: {//时分秒
            switch (component) {
                case 0:
                    _hourIndex = row;
                    break;
                case 1:
                    _minuteIndex = row;
                    break;
                case 2:
                    _secondIndex = row;
                    break;
            }
            break;
        }
    }
    
    NSString *dateStr = [NSString stringWithFormat:@"%@-%@-%@ %@:%@:00",
                         _arrYear[_yearIndex]    ,
                         _arrMonth[_monthIndex]  ,
                         _arrDay[_dayIndex]      ,
                         _arrHour[_hourIndex]    ,
                         _arrMinute[_minuteIndex]
                         //_arrSecond[_secondIndex]
                         ];
    _scrollToDate                  = [RODateHelper dateFromString:dateStr withFormat:nil];
    _datePickerDateDefaultScrollTo = [[RODatePickerDataModel alloc] initWithDate:_scrollToDate];
    
    [pickerView reloadAllComponents];
    
    if (![self validatedDate:_scrollToDate]) {
        [self scrollToDateIndexPositionWithDate:_defaultLimitedDate];
    }
}




@end
